/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Gao zhao xu
 * Date: 2023-10-30
 * Time: 10:42
 */
import java.util.*;
public class Main1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] a = new int[n+1];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        int[] q = new int[n+1];
        int head = 0, tail = 0;
        for (int i = 0; i < n; i++) {
            int x = i - k + 1;
            while (head <= tail && q[head] < x) {
                head++;
            }
            while (tail >= head && a[i] <= a[q[tail]]) {
                tail--;
            }
            q[++tail] = i;
            if (x >= 0){
                System.out.print(a[q[head]] + " ");
            }
        }
        System.out.println();
        q = new int[n+1];
        head = 0;
        tail = 0;
        for (int i = 0; i < n; i++) {
            int x = i - k + 1;
            while (head <= tail && q[head] < x) {
                head++;
            }
            while (tail >= head && a[i] >= a[q[tail]]) {
                tail--;
            }
            q[++tail] = i;
            if (x >= 0){
                System.out.print(a[q[head]] + " ");
            }
        }
    }
}

